package com.scy.java.Test.Test03;
import java.util.Arrays;
//现有数组 int[] arr= {1,2,3,4,5,6,7}。现需删除数组中索引为4的元素
public class Demo01 {
    public static void main(String[] args) {
        //数组
        int[] arr= {1,2,3,4,5,6,7};
        //因为要删除一个数所以新数组的长度为arr数组长度-1
        int[] newArr = new int[arr.length-1];
        //定义一个数组截取索引为4后的数组(4+1)
        int[] endArr =new int[arr.length-(4+1)];
        for (int i = 0; i < endArr.length; i++) {
            //arr数组后两位换给engArr
            endArr[i] =arr[arr.length-i-1];
            //放到新数组的后两位
            newArr[newArr.length-i-1] = endArr[i];
        }
        //截取数组
        arr = Arrays.copyOf(arr,arr.length-(endArr.length+1));
        //截取前四位
        for (int i = 0; i < arr.length; i++) {
            newArr[i] =arr[i];
        }
        //扩容
        arr = new int[arr.length+(endArr.length+1)];
        //赋给扩容后的数组
        arr = Arrays.copyOf(newArr,newArr.length);
        System.out.println("删除后的数组为："+Arrays.toString(arr));
    }
}
